<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./assets/common.css">
  <title>Sankey Diagram</title>
</head>

<body>
<div id="canvas"></div>
<script src="./assets/jquery-3.2.1.min.js"></script>
<script src="../build/g2.js"></script>
<!--<script src="./assets/g2.js"></script>-->
<script src="./assets/data-set.min.js"></script>
<script>
  $.getJSON('./data/energy.json', data => {
    // arc diagram layout
    const ds = new DataSet();
    const dv = ds.createView().source(data, {
      type: 'graph',
      edges: d => d.links
    });
    dv.transform({
      type: 'diagram.sankey'
    });
    const chart = new G2.Chart({
      container: 'canvas',
      forceFit: true,
      height: window.innerHeight
    });
    chart.legend(false);
    chart.tooltip({
      showTitle: false
    });

    chart.scale({
      x: {
        sync: true
      },
      y: {
        sync: true
      }
    });

    // edge view
    const edgeView = chart.view();
    edgeView.source(dv.edges);
    edgeView.axis(false);
    edgeView.edge()
      .position('x*y') // detachment：是否将节点的输入边和输出边权重分开计算的标志
      .shape('arc') // 使用弧线完成边的绘制
      .color('#bbb')
      .opacity(0.6)
      .tooltip('value');

    // node view
    const nodeView = chart.view();
    nodeView.axis(false);
    nodeView.source(dv.nodes);
    nodeView.polygon()
      .position('x*y') // nodes数据的x、y由layout方法计算得出
      .color('name')
      .label('name', {
        // labelEmit: true,
        textStyle: {
          fill: 'black',
          textAlign: 'left'
        },
        offset: 0
      })
      .style({
        stroke: '#ccc'
      });
    chart.render();
  });
</script>
</body>

</html>

